package PagePackage;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import static org.testng.AssertJUnit.assertEquals;

public class leaguePage extends internalPageClass {
	
	
	@FindBy (className="btn-add-new")
	public WebElement addNewLeagueButton;
	public Boolean Boolean;
	
	@FindBy (name="name")
	public WebElement leagueName;
	
	
	@FindBy (className="btn-search")
	public WebElement searchButton;
	

public addLeaguePage clickAddNewLeagueButton() {
	addNewLeagueButton.click();
	return MyPageFactory.getPage(driver, addLeaguePage.class);
}
public addLeaguePage scrollAndClickLeague( final int x, final int y, final String leagueName) {
	
	Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
	        {
	            public Boolean apply( WebDriver webDriver )
	            {
	                try
	                {
	
	WebElement newLeagueClick=driver.findElement(By.xpath(leagueName));
	 
    String code = "window.scroll(" + (newLeagueClick.getLocation().x + x) + ","
                                 + (newLeagueClick.getLocation().y + y) + ");";
 
    ((JavascriptExecutor)driver).executeScript(code, newLeagueClick, x, y); 
    
    
	 
	((JavascriptExecutor) driver).executeScript("arguments[0].click();", newLeagueClick);
	
	                }
	                catch( StaleElementReferenceException e )
	                {
	                	System.out.println( "Select failed! Try again..." );
	                    return false;
	                }
	                System.out.println( "League is found!!" );
	                return true;
	
	            }
	        } );
	
	 return MyPageFactory.getPage(driver, addLeaguePage.class);
    
   
	 
}









public addLeaguePage scrollAndClickLeague2(int x, int y, String leagueName) {
	 boolean present;
	    try {
	WebElement newLeagueClick=driver.findElement(By.xpath(leagueName));
	 
   String code = "window.scroll(" + (newLeagueClick.getLocation().x + x) + ","
                               + (newLeagueClick.getLocation().y + y) + ");";
 
   ((JavascriptExecutor)driver).executeScript(code, newLeagueClick, x, y); 
   
       present = true;
    } catch (NoSuchElementException e) {
       present = false;
    }
	    assertEquals(false, present);
	 return MyPageFactory.getPage(driver, addLeaguePage.class);
	 
}
public leaguePage findLeague(String league){
	leagueName.click();
	leagueName.clear();
	leagueName.sendKeys(league);
	searchButton.click();
	 return MyPageFactory.getPage(driver, leaguePage.class);
}
public boolean isOnThisPage(){
	return addNewLeagueButton.isDisplayed();
}
@Override
void tryToOpen() {
MyPageFactory.getPage(driver, loginPageClass.class).loginAs("victoria@krossover.com", "webmaster");
schooltab.click();
}
}